<?php 
namespace app\common\model;
class OrderDeposit extends Common{
	protected $insert = ['dateline'=>SYS_TIME];
	public function pay_success($ordersn,$userid=0,$price){
		$info = $this->where('ordersn', $ordersn)->find();
		if ($info['status'] >= 1) return false;
		if (!isset($userid) || !$userid) {
			$userid = $info['userid'];
		}
		$uuid = db("Member")->where("id",$userid)->value("uuid");
		if($uuid != 0){
			$url = setAppUrls(url('index/member/amount'),[],'blank');
			jpush([$uuid],'提现到账',$url,"3");
		}
		model("Member")->tixian_audit($userid, $price);
        model("MemberAmountLog")->where('ordersn', $ordersn)->setField('status', 1);
		return $this->where('userid', $userid)->where('ordersn', $ordersn)->update(['status'=>1, 'paytime'=>SYS_TIME]);
	}
	public function pay_fail($ordersn, $userid=0, $price, $description){
		$info = $this->where('ordersn', $ordersn)->find();
		if ($info['status'] == 2) return false;
		if (!isset($userid) || !$userid) {
			$userid = $info['userid'];
		}
		$uuid = db("Member")->where("id",$userid)->value("uuid");
		if($uuid != 0){
			$url = setAppUrls(url('index/member/tixianjilu'),[],'blank');
			jpush([$uuid],'提现失败',$url,"3");
		}
		model("Member")->where('id', $userid)->dec('dis_amount', $price)->inc('amount', $price)->update();
        model("MemberAmountLog")->where('ordersn', $ordersn)->update(['status'=>2, 'description'=>$description]);
		return $this->where('userid', $userid)->where('ordersn', $ordersn)->update(['status'=>2, 'paytime'=>SYS_TIME]);
	}
	protected function getSysManageAttr($value, $data){
		if ($data['status'] != 0) {
			return '';
		}
		return "<a class='cocoAjaxPost' href='Javascript:' confirm='确定要审核通过吗？' ajax-url=".url('tixian')." ajax-data='{  id:".$data['id'].",status:1}'>审核通过</a> | <a class='cocoModal' href=".url("adddesc", ['id'=>$data['id'], 'status'=>2])." isForm  fixat='1' layerwidth='500px' layerheight='380px'>拒绝提现</a>";
	}
	protected function getNicknameAttr($value, $data){
		$info = model("Member")->get_data(['item_id'=>$data['userid']]);
		return $info['nickname'] ? $info['nickname'] : $info['phone'];
	}
	//需要转账的金额
	protected function getTpriceAttr($value, $data){
		//提现手续费
		$deposit_rete = config('DEPOSIT_RATE');
		return round((1 - $deposit_rete / 100) *  $data['price'], 2);
	}
	protected function getStatusAttr($value){
		$arr = [0=>'正在审核', 1=>'提现成功', 2=>'拒绝提现'];
		return $arr[$value];
	}
	protected function getPaynameAttr($value, $data){
		return config("PAYTYPE.".$data['paytype']);
	}
	protected function getDatelineAttr($value){
		return date('Y-m-d H:i:s', $value);
	}
	protected function getPaytimeAttr($value){
		return $value ? date('Y-m-d H:i:s', $value) : '未完成';
	}
	public function tixian_audit($status, $items){
		if ($status == 1) {
			return $this->where('id', 'in', $items)->setField('status', 1);
		}else{
			return $this->where('id', 'in', $items)->setField('status', 2);
		}
	}
	protected function getCardAttr($value, $data){
		return model("MemberCard")->where('userid', $data['userid'])->value('card');
	}
	protected function getRealnameAttr($value, $data){
		return model("MemberRealname")->where('userid', $data['userid'])->value('realname');
	}
	protected function getBanknameAttr($value, $data){
		// $cardtype = model("MemberCard")->where('userid', $data['userid'])->value('cardtype');
		// $arr = config('BANK_LIST');
		// return $arr[$cardtype];
		$arr = config("CARDTYPE");
		return $arr[$data['cardtype']];
	}
	// protected function getCardcityAttr($value, $data){
	// 	$info = model("MemberCard")->where('userid', $data['userid'])->find();
	// 	return $info['province'].' '.$info['city'];
	// }
	public function save_datas($data){
		//提现手续费
		$deposit_rete = config('DEPOSIT_RATE');
		$data['ordersn'] = 'deposit'.SYS_TIME.rand(1000, 9999);
		$data['description'] = '获得金额('.($data['totalprice'] * (1 - $deposit_rete / 100)).') = 提现金额('.$data['totalprice'].') - 手续费('.($data['totalprice'] * $deposit_rete / 100).')';
		// $data['totalprice'] = (1 - $deposit_rete / 100) * $data['totalprice'];
		$id = model("Order")->save_data($data);
		if (!$id) {
			return false;
		}
		$data['price'] = $data['totalprice'];
		$ids = model("OrderDeposit")->save_data($data);
		if (!$ids) {
			return false;
		}
		//冻结资金
		$isok = model("Member")->dis_amount($data['userid'], $data['totalprice']);
		if (!$isok) {
			return false;
		}
		return model('MemberAmountLog')->add($data, -1, 0);
	}
}	